import _mergeWith from 'lodash/mergeWith';

export default function (model = {}) {
  const {
    namespace,
    state = {},
    subscriptions = {},
    effects = {},
    reducers = {}
  } = model;

  if (!namespace) {
    throw new Error('namespace is null!');
  }

  const basicModel = {
    effects: {
      * clean (_, {put}) {
        yield put({type: 'save', payload: state});
      }
    },
    reducers: {
      save (state, action) {
        return {...state, ...action.payload};
      }
    }
  };

  return _mergeWith(basicModel, {
    namespace,
    state,
    subscriptions,
    effects,
    reducers,
  })
}
